package com.ts.api.module.api.service.impl;

import com.gitee.apanlh.util.base.BigDecimalUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.ts.api.common.constant.productbill.ProductBillEnum;
import com.ts.api.common.transaction.TransactionSupport;
import com.ts.api.module.api.dao.ApiKeyBillDao;
import com.ts.api.module.api.dao.ApiKeyDao;
import com.ts.api.module.api.entity.bo.ApiKeyAmountBO;
import com.ts.api.module.api.entity.po.ApiKeyBillPO;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.api.service.ApiKeyAmountService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

@Service
public class ApiKeyAmountServiceImpl implements ApiKeyAmountService {

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private TransactionSupport transactionSupport;
    @Autowired
    private ApiKeyDao apiKeyDao;
    @Autowired
    private ApiKeyBillDao apiKeyBillDao;

    @Override
    public void deduct(ApiKeyAmountBO apiKeyAmountBO) {
        RLock lock = redissonClient.getLock("api_key_bill_lock:" + apiKeyAmountBO.getApiClientId());
        lock.lock(10, TimeUnit.SECONDS);
        try {
            transactionSupport.requiresNew(() -> {
                ApiKeyPO apiKeyPO = apiKeyDao.getByClientId(apiKeyAmountBO.getApiClientId());

                // 获得【扣款类型】
                ProductBillEnum deductType = apiKeyAmountBO.obtainDeductType();
                // 获得【价格】
                BigDecimal price = apiKeyAmountBO.obtainPrice(apiKeyPO.getApiShare(), apiKeyPO.getApiPrice());
                // 【余额】
                BigDecimal balance = apiKeyPO.getBalance();
                // 计算【剩余余额】
                BigDecimal remainingBalance = BigDecimalUtils.minus(balance, price);

                //  记录账单
                ApiKeyBillPO apiKeyBillPO = new ApiKeyBillPO();
                apiKeyBillPO.setLoanProductId(apiKeyAmountBO.getProductId());
                apiKeyBillPO.setLoanProductPlanId(apiKeyAmountBO.getProductPlanId());
                apiKeyBillPO.setLoanUserId(apiKeyAmountBO.getUserId());
                apiKeyBillPO.setPhoneMd5(apiKeyAmountBO.getPhoneMd5());
                apiKeyBillPO.setLoanChannelId(apiKeyAmountBO.getLoanChannelId());
                apiKeyBillPO.setApiClientId(apiKeyAmountBO.getApiClientId());
                apiKeyBillPO.setAmount(price);
                apiKeyBillPO.setAccountBalance(remainingBalance);
                apiKeyBillPO.setCurrentBalance(balance);
                apiKeyBillPO.setType(deductType.getType());
                apiKeyBillPO.setCreateTime(DateUtils.currentTime());
                apiKeyBillDao.insert(apiKeyBillPO);

                //  修改余额
                ApiKeyPO apiKeyPO2 = new ApiKeyPO();
                apiKeyPO2.setApiClientId(apiKeyAmountBO.getApiClientId());
                apiKeyPO2.setBalance(remainingBalance);
                apiKeyDao.updateBalance(apiKeyPO2);
            });
        } finally {
            lock.unlock();
        }
    }

}
